import logging

import requests

from tinyops.library.config import ServerChanNotifierConfig
from tinyops.library.json_helper import shorten_text

from .interface import INotifier


LOG = logging.getLogger(__name__)


class ServerChanNotifier(INotifier):
    def __init__(self, config: ServerChanNotifierConfig):
        self.name = config.name
        self.endpoint = config.endpoint.rstrip('/')
        self.sckey = config.sckey
        self.url = f'{self.endpoint}/{self.sckey}.send'
        self.timeout = config.timeout

    def notify(self, title, content=None):
        LOG.info(f'notify title={title!r} content={shorten_text(content, 40)!r}')
        response = requests.post(self.url, data=dict(title=title, desp=content))
        response.raise_for_status()

    def close(self):
        pass
